package com.zhijian.wharf.service.impl;

import com.zhijian.wharf.dao.DatabaseDeleteMapper;
import com.zhijian.wharf.entity.po.DatabaseDelete;
import com.zhijian.wharf.entity.vo.request.DatabaseDeleteSearchVo;
import com.zhijian.wharf.entity.vo.request.DatabaseDeleteSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.response.DatabaseDeleteVo;
import com.zhijian.wharf.service.DatabaseDeleteService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 数据库清理业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-08-16 18:01:07
 */
@Slf4j
@Service
public class DatabaseDeleteServiceImpl implements DatabaseDeleteService {

	@Autowired
    private DatabaseDeleteMapper mapper;

    @Override
    public DatabaseDelete selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DatabaseDeleteVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(DatabaseDelete databaseDelete) {
        Date now = new Date();
        databaseDelete.setCreateTime(now);
        databaseDelete.setUpdateTime(now);
        return mapper.insert(databaseDelete) > 0 ? databaseDelete.getId() : 0L;
    }

    @Override
    public Long insertSelective(DatabaseDelete databaseDelete) {
        Date now = new Date();
        databaseDelete.setCreateTime(now);
        databaseDelete.setUpdateTime(now);
        return mapper.insertSelective(databaseDelete) > 0 ? databaseDelete.getId() : 0L;
    }

    @Override
    public boolean update(DatabaseDelete databaseDelete) {
        Date now = new Date();
        databaseDelete.setUpdateTime(now);
        return mapper.updateByPrimaryKey(databaseDelete) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(DatabaseDelete record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(DatabaseDeleteSaveOrUpdateVo saveOrUpdateVo) {
        DatabaseDelete databaseDelete = new DatabaseDelete();
        BeanUtils.copyProperties(saveOrUpdateVo, databaseDelete);
        return this.insert(databaseDelete) != 0;
    }

    @Override
    public boolean modify(DatabaseDeleteSaveOrUpdateVo saveOrUpdateVo) {
        DatabaseDelete old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        DatabaseDelete databaseDelete = new DatabaseDelete();
        BeanUtils.copyProperties(saveOrUpdateVo, databaseDelete);
        return this.update(databaseDelete);
    }

    @Override
    public DatabaseDeleteVo selectOne(DatabaseDeleteSearchVo databaseDeleteSearchVo) {
        return mapper.selectOne(databaseDeleteSearchVo);
     }

    @Override
    public Integer count(DatabaseDeleteSearchVo databaseDeleteSearchVo) {
        return mapper.count(databaseDeleteSearchVo);
    }

    @Override
    public List<DatabaseDeleteVo> selectList(DatabaseDeleteSearchVo databaseDeleteSearchVo) {
        return mapper.selectList(databaseDeleteSearchVo);
    }

    @Override
    public Page<DatabaseDeleteVo> selectPage(DatabaseDeleteSearchVo databaseDeleteSearchVo) {
        AssertUtil.check(Objects.isNull(databaseDeleteSearchVo.getPageNum()) || Objects.isNull(databaseDeleteSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(databaseDeleteSearchVo);
        //分页
        Page<DatabaseDeleteVo> page = new Page<>(true, count, databaseDeleteSearchVo.getPageNum(), databaseDeleteSearchVo.getPageSize());
        databaseDeleteSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DatabaseDeleteVo> responseVos = this.selectList(databaseDeleteSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<DatabaseDelete> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(DatabaseDeleteSearchVo databaseDeleteSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(databaseDeleteSearchVo);
    }
    
}